
function normalMonth(m) {
  if(m.length==1){
    return "0"+m;
  }
  return m;
}


function normalYear(y) {
 if(y.length==2){
   if(y < 50) {
     return "20"+y;
   } else {
     return "19"+y;
   }
 }
 return y;
}

function normalDay(d) {
  if(d.length==1){
    return "0"+d;
  }
  return d;
}


function normalDate(d,m,y) {
  return normalDay(d)+"/"+normalMonth(m)+"/"+normalYear(y);
}


function normalizeDate(strdate) {
  if(!strdate) return "";
  // cope with spacenet peculiar way of storing dates
  if(lastchar(strdate)==';') {
    strdate=rmlastchar(strdate);
  }
  var elems=strdate.split("/");
  if(elems.length==3) {
    return normalDate(elems[0],elems[1],elems[2]);
  }
  elems=strdate.split("."); 
  if(elems.length==3) {
    return normalDate(elems[0],elems[1],elems[2]);
  }
  elems=strdate.split("-"); // XP doc date 
  if(elems.length==3) {
    if(elems[0].length==4) { // year at end
      return normalDate(elems[2],elems[1],elems[0]);
    } else {
      return normalDate(elems[0],elems[1],elems[2]);
    }
  }
  
  if(strdate.length==8) { // Viewer format 20041230 
    return normalDate(strdate.substring(6,8),strdate.substring(4,6),strdate.substring(0,4));
  }
  return "error";   
}


function prettyDate(strdate) {
  var elems=strdate.split("/");
  if(elems.length==3) {
    return normalDate(elems[0],elems[1],elems[2]);
  }
  return strdate;
}


function str2Date(strdate) {
 var elems=strdate.split("/");
 var ndate=new Date();
 if(elems.length<3) {
   elems=strdate.split(".");
 }
 if(elems.length==3) {
   ndate.setDate(elems[0]); //alert("Day "+elems[0]+" Month "+elems[1]+" Year "+elems[2]);
   ndate.setMonth(elems[1]-1);
   ndate.setYear(elems[2]);
   return ndate;
 }
 //alert("Empty date "+strdate);
 return "";
} 

function Date2str(ndate) {
  var _ndate=ndate || new Date();
  return normalDate(_ndate.getDate(),(_ndate.getMonth()+1),_ndate.getFullYear());
}
